package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.example.domain.po.*;
import org.example.service.ErpUnitService;
import org.example.service.impl.ErpUnitServiceImpl;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/commodity/unit")
@Api("/计量单位")
public class ErpUnitController {

    @Autowired
    ErpUnitServiceImpl erpUnitServiceimpl;
    @Autowired
    ErpUnitService unitService;
    //分页查询
    @GetMapping("/findByPage")
    public R<Page<ErpUnit>> findByPage (
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            String name,
            @RequestAttribute("user") ErpUser user
    ){
        Page<ErpUnit> page = new Page<>(current, size);
        try {
            page = erpUnitServiceimpl.findByPage(page, name,user.getTenantId() );
            return R.success(page);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }
    }
    //添加
    @PostMapping("/insert")
    public R insert(@RequestBody ErpUnit ErpUnit, @RequestAttribute("user") ErpUser user){
        String basicUnit = ErpUnit.getBasicUnit();
        String otherUnit = ErpUnit.getOtherUnit();
        String otherUnitTwo = ErpUnit.getOtherUnitTwo();
        String otherUnitThree = ErpUnit.getOtherUnitThree();
        BigDecimal ratio = ErpUnit.getRatio();
        BigDecimal ratioTwo = ErpUnit.getRatioTwo();
        BigDecimal RatioThree = ErpUnit.getRatioThree();
        StringBuilder nameBuilder = new StringBuilder();
        nameBuilder.append(basicUnit)
                .append("/(")
                .append(otherUnit)
                .append("=")
                .append(ratio.toString())
                .append(basicUnit)
                .append(")");

        if (otherUnitTwo != null && !otherUnitTwo.isEmpty()) {
            nameBuilder.append("/(")
                    .append(otherUnitTwo)
                    .append("=")
                    .append(ratioTwo.toString())
                    .append(basicUnit)
                    .append(")");
        }
        if (otherUnitThree != null && !otherUnitThree.isEmpty()) {
            nameBuilder.append("/(")
                    .append(otherUnitThree)
                    .append("=")
                    .append(RatioThree.toString())
                    .append(basicUnit)
                    .append(")");
        }

        ErpUnit.setName(nameBuilder.toString());
        ErpUnit.setEnabled(true);
        ErpUnit.setTenantId(user.getTenantId());
        erpUnitServiceimpl.save(ErpUnit);
        return R.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id){
        erpUnitServiceimpl.removeById(id);
        return R.success();
    }
    //修改
    @PostMapping("/update")
    public R update(@RequestBody  ErpUnit ErpUnit){
        String basicUnit = ErpUnit.getBasicUnit();
        String otherUnit = ErpUnit.getOtherUnit();
        String otherUnitTwo = ErpUnit.getOtherUnitTwo();
        String otherUnitThree = ErpUnit.getOtherUnitThree();
        BigDecimal ratio = ErpUnit.getRatio();
        BigDecimal ratioTwo = ErpUnit.getRatioTwo();
        BigDecimal RatioThree = ErpUnit.getRatioThree();
        StringBuilder nameBuilder = new StringBuilder();
        nameBuilder.append(basicUnit)
                .append("/(")
                .append(otherUnit)
                .append("=")
                .append(ratio.toString())
                .append(basicUnit)
                .append(")");

        if (otherUnitTwo != null && !otherUnitTwo.isEmpty()) {
            nameBuilder.append("/(")
                    .append(otherUnitTwo)
                    .append("=")
                    .append(ratioTwo.toString())
                    .append(basicUnit)
                    .append(")");
        }
        if (otherUnitThree != null && !otherUnitThree.isEmpty()) {
            nameBuilder.append("/(")
                    .append(otherUnitThree)
                    .append("=")
                    .append(RatioThree.toString())
                    .append(basicUnit)
                    .append(")");
        }

        ErpUnit.setName(nameBuilder.toString());
        erpUnitServiceimpl.updateById(ErpUnit);
        return R.success();
    }
    @GetMapping("/list")
    public R<List<ErpUnit>> selectByExample(@RequestAttribute("user") ErpUser user){
        List<ErpUnit> list = unitService.selectByExample(user.getTenantId());
        return R.success(list);
    }
    @GetMapping("/select/{id}")
    public R<List<ErpUnit>> select(@PathVariable("id") Integer id, @RequestAttribute("user") ErpUser user){
        List<ErpUnit> units = unitService.select(id, user.getTenantId());
        System.out.println(units);
        return R.success(units);
    }


    //    批量删除
    @DeleteMapping("/delete")
    public R deleteBatch(@RequestBody List<Long> ids) {
        for (Long id : ids) {
            unitService.removeById(id);
        }
        return R.success();
    }
    @PutMapping("/modifyTheEnabled")
    public Result<String> modifyTheState(@RequestBody UpdateEnabled modify){
        boolean theState = unitService.modifyTheState(modify.getIds(), modify.getEnabled());
        return theState ? Result.success("操作成功！") : Result.success("操作失败！");
    }

}
